﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;

using System.Text;
using System.Windows.Forms;
using QUANLYCHAMCONG.BL.Entities;
using Hopthoai;
using QUANLYCHAMCONG.BL.BUS;
using DgvFilterPopup;
using System.IO;
using ValicationCore;
namespace QUANLYCHAMCONG.PL.CMS.Hethong
{
    public partial class Congty : UserControl
    {
        int flag = 0;
        public delegate void _Close();
        public event _Close Close;
        CONGTY cty;
        frmHoidap f;
        string filename;
        public Congty()
        {
            InitializeComponent();
        }
        private void SetLink()
        {
            this.links1.SetValues("Hệ thống", "Thông tin công ty");
        }
        private void Event()
        {

            txtmacty.Focus();
            this.toolbar1.Add += new Toolbar._Add(toolbar1_Add);
            this.toolbar1.Edit += new Toolbar._Edit(toolbar1_Edit);
            this.toolbar1.Save += new Toolbar._Save(Save);
            this.toolbar1.Delete += new Toolbar._Delete(toolbar1_Delete);
            this.toolbar1.Close += new Toolbar._Close(toolbar1_Close);
            this.toolbar1.Refresh += new Toolbar._Refesh(Refresh);
            InitGrid();
            LoadData();
        }

        void Refresh()
        {
            RemoveCheckbox();
            this.toolbar1.SetSave();
            this.flag = 0;
            this.dgvcty.ReadOnly = true;
            this.dgvcty.AllowUserToAddRows = true;
            LoadData();
            this.Clear();
        }
        void toolbar1_Close()
        {
            if (this.Close != null)
                this.Close();
        }
        private int FindAviable()
        {
            foreach (DataGridViewColumn dr in this.dgvcty.Columns)
            {
                if (dr is DataGridViewCheckBoxColumn)
                    return 1;
            }
            return 0;
        }
        private void RemoveCheckbox()
        {
            if (FindAviable() == 1)
            {
                this.dgvcty.Columns.RemoveAt(0);
                this.dgvcty.Controls.RemoveByKey(((CheckBox)this.dgvcty.Controls.Find("checkboxHeader", true)[0]).Name);
                LoadData();
            }
        }
        private void AddCheckbox(DataTable dt)
        {
            if (FindAviable() == 0)
            {
                DataGridViewCheckBoxColumn cb = new DataGridViewCheckBoxColumn();
                cb.Width = 30;
                cb.FillWeight = 30;
                cb.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
                this.dgvcty.Columns.Insert(0, cb);
                Rectangle rect = this.dgvcty.GetCellDisplayRectangle(0, -1, true);
                rect.X = rect.Location.X + cb.Width / 33;
                CheckBox checkboxHeader = new CheckBox();
                checkboxHeader.Name = "checkboxHeader";
                checkboxHeader.Size = new Size(18, 18);
                checkboxHeader.Location = rect.Location;
                checkboxHeader.CheckedChanged += new EventHandler(checkboxHeader_CheckedChanged);
                this.dgvcty.Controls.Add(checkboxHeader);
                this.dgvcty.Columns[0].Width = 50;
                this.dgvcty.DataSource = dt;
            }
        }

        void checkboxHeader_CheckedChanged(object sender, EventArgs e)
        {
            if (((CheckBox)this.dgvcty.Controls.Find("checkboxHeader", true)[0]).Checked == true)
            {
                for (int i = 0; i < this.dgvcty.RowCount; i++)
                {
                    this.dgvcty[0, i].Value = ((CheckBox)this.dgvcty.Controls.Find("checkboxHeader", true)[0]).Checked;
                }
            }
            else
            {
                for (int i = 0; i < this.dgvcty.RowCount; i++)
                {
                    this.dgvcty[0, i].Value = false;
                }
            }
        }
        void toolbar1_Delete()
        {
            flag = 3;
            this.dgvcty.ReadOnly = false;
            this.dgvcty.AllowUserToAddRows = false;
            this.toolbar1.SetAdd_Edit_Delete();
            AddCheckbox(CONGTYBUS.SelectAll());

        }

        void toolbar1_Edit()
        {
            flag = 2;
            Clear();
            this.dgvcty.Focus();
            this.dgvcty.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
            this.dgvcty.Rows[0].Selected = true;
            this.dgvcty.ReadOnly = false;
            this.dgvcty.AllowUserToAddRows = false;
            this.toolbar1.SetAdd_Edit_Delete();
        }

        void toolbar1_Add()
        {
            flag = 1;
            Clear();
            CreateID();
            this.toolbar1.SetAdd_Edit_Delete();
            txttencty.Focus();

        }
        CONGTY AddDuLieu(CONGTY ctys)
        {
            ctys.MACTY = txtmacty.Text;
            ctys.TENDKY = txttencty.Text;
            ctys.TENVIETTAT = txttenvt.Text;
            ctys.CHINHANH = int.Parse(txtsochinhanh.Text);
            ctys.DIACHI = txtdiachi.Text;
            ctys.EMAIL = txtemail.Text;
            ctys.FAX = txtfax.Text;
            ctys.MALCTY = cmbloaicty.SelectedValue.ToString();
            ctys.MASOTHUE = txtmasothue.Text;
            ctys.SODT = txtsdt.Text;
            ctys.SOTK = txtsotk.Text;
            ctys.TENNGANHANG = txttennh.Text;
            ctys.WEBSITE = txtweb.Text;
            ctys.LOGO = filename;
            this.filename = "";
            return ctys;
        }
        void Save()
        {
            //khoi tao
            cty = new CONGTY();
            if (flag == 1)
            {
                if (CONGTYBUS.Insert(AddDuLieu(cty)) == true)
                {
                    f = new frmHoidap("Thêm cty làm việc thành công!", frmHoidap.CO.an, frmHoidap.KHONG.an, frmHoidap.THOAT.an, frmHoidap.HINH.thongtin);
                    f.ShowDialog();
                    LoadData();
                    this.toolbar1.SetSave();
                    Clear();
                }
                else
                {
                    f = new frmHoidap("Thêm cty làm việc có lỗi xảy ra!", frmHoidap.CO.an, frmHoidap.KHONG.an, frmHoidap.THOAT.an, frmHoidap.HINH.baoloi);
                    f.ShowDialog();
                    LoadData();
                    this.toolbar1.SetSave();
                    Clear();
                }
            }
            if (flag == 2)
            {
                try
                {
                    f = new frmHoidap("Bạn có muốn sửa cty làm việc  ?", frmHoidap.CO.hien, frmHoidap.KHONG.hien, frmHoidap.THOAT.an, frmHoidap.HINH.canhbao);
                    if (f.ShowDialog() == DialogResult.Yes)
                    {
                        if (CONGTYBUS.Update(AddDuLieu(cty)) == true)
                        {
                            f = new frmHoidap("Sửa cty làm việc thành công!", frmHoidap.CO.an, frmHoidap.KHONG.an, frmHoidap.THOAT.an, frmHoidap.HINH.thongtin);
                            f.ShowDialog();
                            LoadData();
                            this.dgvcty.ReadOnly = true;
                            this.toolbar1.SetSave();
                            Clear();
                        }
                        else
                        {
                            f = new frmHoidap("Có lỗi khi Sửa cty làm việc !", frmHoidap.CO.an, frmHoidap.KHONG.an, frmHoidap.THOAT.an, frmHoidap.HINH.baoloi);
                            f.ShowDialog();
                            this.toolbar1.SetSave();
                            this.dgvcty.ReadOnly = true;
                            Clear();
                        }
                    }
                    else
                    {
                        this.dgvcty.ReadOnly = true;
                        this.toolbar1.SetSave();
                        Clear();
                    }
                }
                catch { return; }
            }
            if (flag == 3)
            {
                f = new frmHoidap("Bạn có chắc chắn muốn xóa dữ liệu cty làm việc vừa chọn?", frmHoidap.CO.hien, frmHoidap.KHONG.hien, frmHoidap.THOAT.an, frmHoidap.HINH.dauhoi);
                if (f.ShowDialog() == DialogResult.Yes)
                {
                    foreach (DataGridViewRow dr in this.dgvcty.Rows)
                    {
                        try
                        {
                            if (dr.Cells[0].Value != null)
                            {
                                cty = new CONGTY();
                                cty.MACTY = dr.Cells["MACTY"].Value.ToString();
                                CONGTYBUS.Delete(cty);
                            }
                        }
                        catch
                        {
                            f = new frmHoidap("Bạn chưa chọn phần cty làm việcnào vui lòng chọn lại!", frmHoidap.CO.an, frmHoidap.KHONG.an, frmHoidap.THOAT.an, frmHoidap.HINH.thongtin);
                            f.ShowDialog();
                        }
                    }
                    f = new frmHoidap("Xóa thành công cty làm việc!", frmHoidap.CO.an, frmHoidap.KHONG.an, frmHoidap.THOAT.an, frmHoidap.HINH.thongtin);
                    f.ShowDialog();
                    RemoveCheckbox();
                    this.toolbar1.SetSave();
                    this.dgvcty.ReadOnly = true;
                    this.dgvcty.AllowUserToAddRows = true;
                }

            }

        }
        void CreateID()
        {
            cty = new CONGTY();
            this.txtmacty.Text = CONGTYBUS.GetID();
        }
        private void Clear()
        {
            foreach (Control c in this.Controls)
            {
                if (c is Panel)
                {
                   
                            foreach (Control d in c.Controls)
                                if (d is TextBox)
                                    d.Text = "";
                    }
                }
            this.piclogo.Image = global::QUANLYCHAMCONG.Properties.Resources.images;
            this.txtsdt.Text = "0";
            this.txtfax.Text = "0";
            this.txtsochinhanh.Text = "0";
            this.txtsotk.Text = "0";
            this.txtmasothue.Text = "0";
        }
        void LoadData()
        {
            try
            {
                cty = new CONGTY();
                Common.HightLightDataGridViewRow(ref this.dgvcty, CONGTYBUS.SelectAll());
                this.dgvcty.Columns["MACTY"].Width = 100;
                this.dgvcty.Columns["LOGO"].Visible = false;
                this.dgvcty.Columns["TENVIETTAT"].Visible = false;
                this.dgvcty.Columns["EMAIL"].Visible = false;
                this.dgvcty.Columns["FAX"].Visible = false;
                this.dgvcty.Columns["MASOTHUE"].Visible = false;
                this.dgvcty.Columns["CHINHANH"].Visible = false;
                this.dgvcty.Columns["SOTK"].Visible = false;
                LoadCombobox();
            }
            catch { return; }
        }
        void LoadCombobox()
        {
            LOAICTY l = new LOAICTY();
            this.cmbloaicty.DataSource = LOAICTYBUS.SelectAll();
            this.cmbloaicty.DisplayMember = "TENLCTY";
            this.cmbloaicty.ValueMember = "MALCTY";
        }
        private void InitGrid()
        {
            DgvFilterManager f = new DgvFilterManager();
            f.DataGridView = (DataGridView)this.dgvcty;
        }

        void ChooseData(object sender, int e)
        {
            if (flag == 2)
            {
                try
                {

                    this.dgvcty.EndEdit();
                    DataGridViewRow dr = this.dgvcty.Rows[e];
                    txtmacty.Text = dr.Cells["MACTY"].Value.ToString();
                    txttencty.Text = dr.Cells["TENDKY"].Value.ToString();
                    txttenvt.Text = dr.Cells["TENVIETTAT"].Value.ToString();
                    txtdiachi.Text = dr.Cells["DIACHI"].Value.ToString();
                    txtweb.Text = dr.Cells["WEBSITE"].Value.ToString();
                    txtemail.Text = dr.Cells["EMAIL"].Value.ToString();
                    txtsdt.Text = dr.Cells["SODT"].Value.ToString();
                    txtfax.Text = dr.Cells["FAX"].Value.ToString();
                    cmbloaicty.SelectedIndex =this.cmbloaicty.FindString(dr.Cells["TENLCTY"].Value.ToString());
                    txtsochinhanh.Text = dr.Cells["CHINHANH"].Value.ToString();
                    txtsotk.Text = dr.Cells["SOTK"].Value.ToString();
                    txtmasothue.Text = dr.Cells["MASOTHUE"].Value.ToString();
                    txttennh.Text = dr.Cells["TENNGANHANG"].Value.ToString();
                    if (String.IsNullOrEmpty(dr.Cells["LOGO"].Value.ToString()))
                    {
                        this.piclogo.Image = global::QUANLYCHAMCONG.Properties.Resources.images;
                        this.filename = null;
                    }
                    else
                    {
                        this.piclogo.Image = Image.FromFile(dr.Cells["LOGO"].Value.ToString());
                        this.filename = dr.Cells["LOGO"].Value.ToString();
                    }
                }
                catch { return; };
            }
        }

        private void piclogo_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            if (this.flag == 2 || this.flag == 1 )
            {
                try
                {
                    OpenFileDialog open = new OpenFileDialog();
                    open.Filter = "Image Files(*.BMP;*.JPG;*.JPEG;*.GIF)|*.BMP;*.JPG;*.JPEG;*.GIF|All files (*.*)|*.*";
                    open.Multiselect = false;
                    open.ShowDialog();
                    if (!String.IsNullOrEmpty(open.FileName))
                    {
                        string tenfile = open.SafeFileName;
                        filename = open.FileName;
                        string current = Application.StartupPath;
                        string chuoi=current + "\\App_Image\\Logo\\" + tenfile;
                        File.Copy(filename, chuoi);
                        this.piclogo.Image = Image.FromFile(chuoi);
                       
                    }
                }
                catch
                {
                    return;
                }
            }
        }

        private void Congty_Load(object sender, EventArgs e)
        {
            Event();
            SetLink();
        }

        private void txtemail_Leave(object sender, EventArgs e)
        {
            if (this.flag != 0)
            {
                if (Valication.CheckEmail(txtemail.Text) == false)
                {
                    f = new frmHoidap("Bạn nhập địa chỉ mail sai! vui lòng nhập lại", frmHoidap.CO.hien, frmHoidap.KHONG.hien, frmHoidap.THOAT.an, frmHoidap.HINH.baoloi);
                    if (f.ShowDialog() == DialogResult.Yes)
                    {
                        txtemail.Text = "";
                        txtemail.Focus();
                    }
                    else
                    {
                    }
                }
            }
        }

        private void dgvcty_CellBeginEdit(object sender, DataGridViewCellCancelEventArgs e)
        {
            ChooseData(sender, e.RowIndex);
        }

        private void dgvcty_CellStateChanged(object sender, DataGridViewCellStateChangedEventArgs e)
        {
            ChooseData(sender, 0);
        }

        private void dgvcty_RowEnter(object sender, DataGridViewCellEventArgs e)
        {
            ChooseData(sender, e.RowIndex);
        }


    }
}
